package GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import Client.BusinessImplements;
import Controler.Update_Subject_Controler;
import javax.swing.border.LineBorder;
/**
 * @author Lucie
* Class name Update_Subject_Panel_first
* extend  Abstract_Jpanel
* purpose: choose an subject to change his details
*/
public class Update_Subject_Panel_first extends Abstract_Jpanel {
	private JComboBox comboBoxsubject;
	private String[] all_subject_name;
	private String[] all_subject_id;
	private ArrayList<String> all_subject;
	
	/**
	 * Create the panel.
	 */
	public Update_Subject_Panel_first() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(SystemColor.activeCaption, 6));
		comboBoxsubject = new JComboBox();
		panel.setBackground(SystemColor.inactiveCaption);
		panel.setBounds(39, 52, 528, 230);
		add(panel);
		panel.setLayout(null);
		
		JLabel lblsubjectName = new JLabel("subject name:");
		lblsubjectName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblsubjectName.setBounds(75, 45, 107, 23);
		panel.add(lblsubjectName);
		
		 
		panel.add(comboBoxsubject);
		comboBoxsubject.setBounds(192, 48, 160, 20);
		
		JLabel lblChoosesubjectYou = new JLabel("choose subject you will like to change hes deatails");
		lblChoosesubjectYou.setForeground(Color.RED);
		lblChoosesubjectYou.setFont(new Font("Segoe Print", Font.PLAIN, 13));
		lblChoosesubjectYou.setBounds(85, 84, 334, 23);
		panel.add(lblChoosesubjectYou);
		
		JLabel lblChoosesubject = new JLabel("choose subject  to update");
		lblChoosesubject.setForeground(new Color(25, 25, 112));
		lblChoosesubject.setFont(new Font("MV Boli", Font.BOLD, 30));
		lblChoosesubject.setBounds(47, 11, 558, 37);
		add(lblChoosesubject);
		//go to next screen "Update_Author_Panel_sec" and send to this screen the 
		// subject the user chose
		JButton btnGo = new JButton("go");
		btnGo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!get_subject_id().equals(""))
				{
				Update_Subject_Panel_sec update_subject=new Update_Subject_Panel_sec();
		  		on_screen_unload();
		  		BusinessImplements.GetInstance().getMainFrame().setContentPane(update_subject);
		  		update_subject.setSize(getMaximumSize());
		  		update_subject.on_screen_load(get_subject_id(),get_subject_name());
				}
				else
					error_msg();
					
				
				
			}
		});
		btnGo.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnGo.setBounds(474, 312, 92, 37);
		add(btnGo);
		//go back to sing in panel
		JButton btnBack = new JButton("back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Sign_in_panel sip=new Sign_in_panel();
				on_screen_unload();
				BusinessImplements.GetInstance().getMainFrame().setContentPane(sip);
				sip.setSize(getMaximumSize());
				sip.on_screen_load();
				
				
			}
		});
		btnBack.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnBack.setBounds(66, 312, 92, 37);
		add(btnBack);

	}

	@Override
	public void next_screen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prev_screen() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * this method is the first method we call in this screen
	 * the main purpose of this method is to initialize the comboBox 
	 * that this panel use, here we call the controller of this panel
	 * to get all subject names and id's and set them at the combobox
	 */
	public void on_screen_load() {
		

		
		Update_Subject_Controler update_subject_controler=new  Update_Subject_Controler();
		all_subject=update_subject_controler.get_all_subject();
		
		String[] line;
		all_subject_name = new String[all_subject.size()+1];
		all_subject_id = new String[all_subject.size()+1];
		all_subject_name[0] = "";
		all_subject_id[0] =  "";
		for (int i = 1 ; i <= all_subject.size() ; i++)
		{
			line = all_subject.get(i-1).split(",");
			all_subject_id[i] = line[0];
			all_subject_name[i] = line[1];
			
		}
		
		comboBoxsubject.setModel(new DefaultComboBoxModel(all_subject_name));
		
		
	}

	@Override
	public void on_screen_unload() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * @return subject id of the author that was chosen in the combobox
	 */
	public String get_subject_id()
	{
		
		if (!comboBoxsubject.getSelectedItem().equals(""))
			{
				for(int i=0;i< all_subject_name.length;i++)
					if( all_subject_name[i].equals(comboBoxsubject.getSelectedItem()))
						
						return all_subject_id[i];
				   	
			
			}
		
		return "";
	}
	/**
	 * 
	 * @return subject name of the author that was chosen in the combobox
	 */
	public String get_subject_name()
	{
		
		if (!comboBoxsubject.getSelectedItem().equals(""))
			{
				for(int i=0;i< all_subject_name.length;i++)
					if( all_subject_name[i].equals(comboBoxsubject.getSelectedItem()))
						
						return all_subject_name[i];
				   	
			
			}
		
		return "";
	}
	/**
	 * this method display showMessageDialog with a failed msg
	 */
	
	public void error_msg()
	{
	JOptionPane.showMessageDialog(this,"miss subject deatails ");
	}
}
